package cn.yougou.serviceImpl;

import cn.yougou.mapper.YgCategoryMapper;
import cn.yougou.pojo.YgCategory;
import cn.yougou.service.admin.YgAdminCategoryService;
import cn.yougou.vo.YgCategoryVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 后台管理商品模块
 */
@Service
public class YgAdminCategoryServiceImpl implements YgAdminCategoryService {

    @Autowired
    private YgCategoryMapper ygCategoryMapper;


    @Override
    public YgCategory queryById(Integer id) {
        return ygCategoryMapper.queryById(id);
    }

    /**
     * 查询全部分类
     * @param ygCategory 实例对象
     * @return
     * @throws Exception
     */
    public List<YgCategoryVo> categoryAll(YgCategory ygCategory) throws Exception {
        List<YgCategoryVo> aTitle = null;
        if(ygCategory.getPid() == 0){
            aTitle = ygCategoryMapper.categoryAll(ygCategory);
            for (YgCategoryVo atitle : aTitle) {
                List<YgCategoryVo> bTitle = ygCategoryMapper.categoryAll(new YgCategory(atitle.getId()));
                atitle.setChildren(bTitle);
                for (YgCategoryVo btitle :bTitle){
                    List<YgCategoryVo> cTitle = ygCategoryMapper.categoryAll(new YgCategory(btitle.getId()));
                    //Lambda表达式
                    //cTitle.forEach(empty -> empty.setLevel("L3"));
                    btitle.setChildren(cTitle);
                }
            }
            return aTitle;
        }else{
            return aTitle;
        }
    }


    /**
     * 根据实体类查询
     * @param ygCategory
     * @return
     * @throws Exception
     */
    @Override
    public List<YgCategoryVo> categoryByCondition(YgCategory ygCategory) throws Exception {
        return ygCategoryMapper.categoryAll(ygCategory);
    }


    /**
     * 新增类目
     * @param ygCategory
     * @return
     */
    @Override
    public int creation(YgCategory ygCategory) {
        return ygCategoryMapper.insert(ygCategory);
    }


    /**
     * 删除类目（更改删除逻辑）
     * @param ygCategory
     * @return
     */
    @Override
    public int delete(YgCategory ygCategory) {
        return ygCategoryMapper.delete(ygCategory);
    }


    /**
     * 更改类目
     * @param ygCategory
     * @return
     */
    @Override
    public int update(YgCategory ygCategory) {
        return ygCategoryMapper.update(ygCategory);
    }


//    @Scheduled(fixedDelay = 1000)//一秒执行一次
//    public void test(){
//        System.out.println("定时器触发");
//    }


}
